
//
//  IOSDeviceConfig.m
//  CommonLibrary
//
//  Created by AlexiChen on 13-1-11.
//  Copyright (c) 2013年 AlexiChen. All rights reserved.
//

#import "IOSDeviceConfig.h"
#import "IOSDeviceMacro.h"
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>

@implementation IOSDeviceConfig

static IOSDeviceConfig *_sharedConfig = nil;


@synthesize isIPad = _isIPad;
@synthesize isIOS7 = _isIOS7;
@synthesize isIOS6 = _isIOS6;
@synthesize isIOS6Later = _isIOS6Later;


//@synthesize deviceUUID = _deviceUUID;

+ (IOSDeviceConfig *)sharedConfig
{
    @synchronized(_sharedConfig)
    {
        if (_sharedConfig == nil) {
            _sharedConfig = [[IOSDeviceConfig alloc] init];
        }
        return _sharedConfig;
    }
}

- (void)dealloc
{
//    [_deviceUUID release];
    CommonRelease(_deviceUUID);
    CommonSuperDealloc();
//    [super dealloc];
}


- (id)init
{
	if (self = [super init])
	{
        _isIPad = isIPad();
        _isIOS7 = isIOS7();
        _isIOS6 = isIOS6();
        _isIOS6Later = [[UIDevice currentDevice].systemVersion doubleValue]>= 7.0;
        _isIOS7Later = [[UIDevice currentDevice].systemVersion doubleValue]>= 8.0;
        
	}
	return self;
}

- (BOOL)isPortrait
{
    return UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation);
}


+ (NSString*)getIPAddressByHostName:(NSString*)strHostName
{
    const char* szname = [strHostName UTF8String];
    struct hostent* phot ;
    @try
    {
        phot = gethostbyname(szname);
    }
    @catch (NSException * e)
    {
        return nil;
    }
    
    struct in_addr ip_addr;
    memcpy(&ip_addr,phot->h_addr_list[0],4);
    //h_addr_list[0]里4个字节,每个字节8位，此处为一个数组，一个域名对应多个ip地址或者本地时一个机器有多个网卡
    
    char ip[20] = {0};
    inet_ntop(AF_INET, &ip_addr, ip, sizeof(ip));
    
    NSString* strIPAddress = [NSString stringWithUTF8String:ip];
    return strIPAddress;
}

@end
